package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.service.IYyTeacherService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.YyClass;
import com.ruoyi.system.service.IYyClassService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 班级Controller
 *
 * @author ruoyi
 * @date 2024-03-12
 */
@Controller
@RequestMapping("/system/class")
public class YyClassController extends BaseController
{
    private String prefix = "system/class";

    @Autowired
    private IYyClassService yyClassService;

    @Autowired
    private IYyTeacherService yyTeacherService;

    @RequiresPermissions("system:class:view")
    @GetMapping()
    public String classs()
    {
        return prefix + "/class";
    }

    /**
     * 查询班级列表
     */
    @PostMapping("/listAll")
    @ResponseBody
    public AjaxResult listAll()
    {
        List<YyClass> list = yyClassService.listAll();
        return AjaxResult.success(list);
    }


    /**
     * 查询班级列表
     */
    @RequiresPermissions("system:class:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YyClass yyClass)
    {
        startPage();
        List<YyClass> list = yyClassService.selectYyClassList(yyClass);
        return getDataTable(list);
    }

    /**
     * 导出班级列表
     */
    @RequiresPermissions("system:class:export")
    @Log(title = "班级", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YyClass yyClass)
    {
        List<YyClass> list = yyClassService.selectYyClassList(yyClass);
        ExcelUtil<YyClass> util = new ExcelUtil<YyClass>(YyClass.class);
        return util.exportExcel(list, "班级数据");
    }

    /**
     * 新增班级
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("tea", yyTeacherService.listAll());
        return prefix + "/add";
    }

    /**
     * 新增保存班级
     */
    @RequiresPermissions("system:class:add")
    @Log(title = "班级", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YyClass yyClass)
    {
        return toAjax(yyClassService.insertYyClass(yyClass));
    }

    /**
     * 修改班级
     */
    @RequiresPermissions("system:class:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        YyClass yyClass = yyClassService.selectYyClassById(id);
        mmap.put("yyClass", yyClass);
        mmap.put("tea", yyTeacherService.selectYyTeacherByIdList(yyClass.getTeaId()));
        return prefix + "/edit";
    }

    /**
     * 修改保存班级
     */
    @RequiresPermissions("system:class:edit")
    @Log(title = "班级", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YyClass yyClass)
    {
        return toAjax(yyClassService.updateYyClass(yyClass));
    }

    /**
     * 删除班级
     */
    @RequiresPermissions("system:class:remove")
    @Log(title = "班级", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(yyClassService.deleteYyClassByIds(ids));
    }
}
